<?php
namespace app\index\controller;

use \think\Controller;
//use think\facade\Request;
use think\Db;
use think\facade\Request;
use app\index\controller\Base;
class Pay extends  Base
{
	public function index()
	{
		return $this->fetch();
	}
	public function directpay()
	{
		if(!Session::has('userinfo'))
		{
			echo 40001;
			exit;
		}else
		{
			$derectpay = input('post.');
	//		print_r($derectpay);
	//		exit;
			session('derectpay',$derectpay);
			echo 40000;
		}		
	}
	public function confirm_order()
	{
		$userinfo = session('userinfo');
		
		//用户地址信息
		$address = Db::table('shop_user_details')
		->field('shop_user_details.id as id,phone,region_name,region_details')
		->join('shop_region','shop_user_details.region_id=shop_region.id')
		->where('user_id',$userinfo['id'])
		->select();	
		
		//直接购买商品信息
		$directbuy = session('derectpay');
		$buy_id = $directbuy['buy_id'];
		$buy_num = $directbuy['buy_num'];
		$style_id = $directbuy['buy_style'];
		$buy_good = Db::table('shop_goods')
		->field('shop_goods.id as id,shop_goods_detail.id as detail_id,goods_summary,goods_color,goods_price,discount,goods_thumb')
		->join('shop_goods_cate','shop_goods.goods_cate_id=shop_goods_cate.id','left')
		->join('shop_goods_detail','shop_goods.id=shop_goods_detail.shop_goods_id','left')
		->where('shop_goods_detail.id',$style_id)
		->find();
		$price = substr($buy_good['goods_price'], 3);
		$this->assign('price',$price);
		$this->assign('buy_id',$buy_id);
		$this->assign('style_id',$style_id);
		$this->assign('buy_num',$buy_num);
		$this->assign('address',$address);
		$this->assign('userinfo',$userinfo);
		$this->assign('buy_good',$buy_good);
//		dump($price);
//		exit;
		return $this->fetch('confirm_order');
	}
	
	public function buy()
	{
		$post = input('post.');
		$goods_id = $post['buy_id'];
		$style_id = $post['buy_style'];
		$goods_num = $post['buy_num'];
		$user_detail_id = $post['detail_id'];
		$totalprice = $post['totalprice'];
		$status = 0;
		
		$ordernum = date('Ymdhis',time()).mt_rand(10000,99999);
		session('derectpay',null);
		$id = Db::table('shop_order')->insertGetId([
			'goods_id'=>$goods_id,
			'style_id'=>$style_id,
			'goods_num'=>$goods_num,
			'user_detail_id'=>$user_detail_id,
			'ordernum'=>$ordernum,
			'totalprice'=>$totalprice,
			'status'=>$status
		]);
		if($id>0)
		{
			echo $ordernum;
			exit;
		}else
		{
			echo 0;
			exit;
		}
		
		// 启动事务
//		Db::startTrans();
//		try {
//		   
//			Db::table('shop_goods_detail')
//			->where('id',$style_id) 
//			->setDec('goods_num',$goods_num);
//			
//		    // 提交事务
//		    Db::commit();
//		} catch (\Exception $e) {
//		    // 回滚事务
//		    Db::rollback();
//			echo 50001;
//			exit;
//		}
		
	}

	public function pay()
	{
		$orderid = input('orderid');
		$order = Db::table('shop_order')
		->where('ordernum',$orderid)
		->find();
		$totalprice = $order['totalprice'];
//		echo $order;exit;
		$this->assign('totalprice',$totalprice);
//		$this->assign('orderid',$orderid);
		return $this->fetch();
	}

	public function successpay()
	{
		return	$this->fetch();
	}

}